var $,
    form,
    layer,
    upload;

var user = JSON.parse(localStorage.getItem('user'));
layui.use(['jquery', 'form', 'layer', 'upload'], function () {
    $ = layui.jquery,
        form = layui.form,
        layer = layui.layer,
        upload = layui.upload;


    //图片上传
    upload.render({
        elem: '#imgUpload'
        ,url: '/Upload/uploadImages'
        ,auto: false
        ,bindAction: '#subUpload'
        ,accept: 'images'
        ,field: 'files'
        ,choose:function (obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#upload1Img').attr('src', result)
            });
        }
        ,done: function(res){
            //上传完毕
            $('input[name=url]').val(res.data);
        }
    });

    //视频上传
    upload.render({
        elem: '#videoUpload'
        ,url: '/Upload/uploadImages'
        ,auto: false
        ,bindAction: '#Upload'
        ,accept: 'video'
        ,field: 'files'
        ,choose:function (obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#uploadVideo').attr('src', result)
            });
        }
        ,done: function(res){
            //上传完毕
            $('input[name=courseUrl]').val(res.data);
        }
    });

    //添加
    form.on('submit(formDemo)', function(data){
        //触发上传
        $('#subUpload').click();
        $('#Upload').click();
        setTimeout(function () {
            //获取时间
            var date = getDate();
            data = form.val('form');
            console.log(data.courseStatus);
            data.userId = user.userId;
            data.recommend = 2;
            data.coursePrice = 0;
            if (data.courseStatus == 'on'){
                data.courseStatus = 2;
            } else {
                data.courseStatus = 3;
            }
            data.courseCstatus = 3;
            data.courseNo = 1;
            data.courseTime = 60;
            data.likeNum = 0;
            data.shareNum = 0;
            data.collectNum = 0;
            data.lookNum = 0;
            data.saleNum = 0;
            data.id = 1001;
            data.createId = user.userId;
            data.createDate = date;
            data.updateId = user.userId;
            data.updateDate = date;
            data.applyDate = date;
            data.launchId = user.userId;
            data.result = 2;
            data.type = 2;
            $.ajax({
                url:'/Course/addCourse',
                type: 'post',
                dataType:'json',
                data: data,
                success:function (data) {
                    if(data.data){
                        // 添加的操作日志
                        $.ajax({
                            url:'/Product/addProductOperationRecord',
                            dataType:'json',
                            data:{
                                "operationType": "1",
                                "executionTime": date,
                                "remarks": "添加课程信息",
                                "updateId": user.userId,
                                "operationList": 'js_course',
                                "type": 2
                            }
                        });
                        layer.msg("添加成功");
                    }else{
                        layer.msg("添加失败");
                    }
                }
            });
        }, 2000 )
        return false;
    });

});

function findBookName () {
    layer.open({
        type: 2,
        offset: 'auto',
        title: '信息',
        resize: false,
        area: ['670px', '500px'],
        content: '/course/courseList-add-layer/courseList-category.html'
    });
};

//获取时间
function getDate() {
    var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
    var temp = date.substring(0, 9);
    date = date.substr(date.lastIndexOf('-') + 3);
    date = temp + date;
    return date;
}

//返回首页
function backOff(){
    parent.jumpSystemHome();
}